package cn.stylefeng.guns.modular.business.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.business.entity.Material;
import cn.stylefeng.guns.modular.business.model.params.MaterialParam;
import cn.stylefeng.guns.modular.business.service.MaterialService;
import cn.stylefeng.guns.sys.core.util.StringUtils;
import cn.stylefeng.guns.sys.modular.system.service.FileInfoService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.util.HtmlUtils;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;


/**
 * 推广素材控制器
 *
 * @author pzp
 * @Date 2019-07-30 17:24:57
 */
@Controller
@RequestMapping("/material")
public class MaterialController extends BaseController {

    private String PREFIX = "/business/material";

    @Autowired
    private MaterialService materialService;

    @Autowired
    private FileInfoService fileInfoService;

    @Value("${server.port}")
    private String port;

    /**
     * 跳转到主页面
     *
     * @author pzp
     * @Date 2019-07-30
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/material.html";
    }

    /**
     * 新增页面
     *
     * @author pzp
     * @Date 2019-07-30
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/material_add.html";
    }

    /**
     * 编辑页面
     *
     * @author pzp
     * @Date 2019-07-30
     */
    @RequestMapping("/edit")
    public String edit(String materialId, Model model) {
        Material detail = this.materialService.getById(materialId);
        model.addAttribute("detail",detail);
        return PREFIX + "/material_edit.html";
    }

    /**
     * 新增接口
     *
     * @author pzp
     * @Date 2019-07-30
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(MaterialParam materialParam) {
        this.materialService.add(materialParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author pzp
     * @Date 2019-07-30
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(MaterialParam materialParam) {
        if (StringUtils.isNotEmpty(materialParam.getContent())) {
            materialParam.setContent(HtmlUtils.htmlUnescape(materialParam.getContent()));
        }
        this.materialService.update(materialParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author pzp
     * @Date 2019-07-30
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(MaterialParam materialParam) {
        this.materialService.delete(materialParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author pzp
     * @Date 2019-07-30
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(MaterialParam materialParam) {
        Material detail = this.materialService.getById(materialParam.getMaterialId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author pzp
     * @Date 2019-07-30
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(String type, String beginTime, String endTime) {
        return this.materialService.findPageBySpec(type, beginTime, endTime);
    }
}


